*
use temp1,clear
keep firmid year naf2 valueadded* *pty* employment ltradeshock6 shock6intens exportintens* sales exp_sales totexport expintens_quant linv_d1 lmatraw_d1 yd*
sort firmid year
tsset firmid year
*
egen emp_sum = sum(employment) , by(year)
gen emp_shr = employment/emp_sum
gen expintens_emp = exportintenscur*emp_shr
gen ltradeshock6_emp = ltradeshock6*emp_shr
gen shock6intens_emp = shock6intens*emp_shr
gen laborpty_emp = laborpty*emp_shr
gen laborpty_def_emp = laborpty_def*emp_shr
*
global myvarlist "sales exp_sales totexport valueadded* employment laborpty*_emp"
tabstat $myvarlist, by(year) stat(sum) nototal
*
save temp1_bottomline , replace
*
***********************************
*UN-Deflated bottom line  with FEs*
***********************************
use temp1_bottomline , clear
set more off
egen naf2yr = group(naf2 year)
drop if employment == 0
*drop if missing(exportintenslag)
*
gen expintens_quant_annual = 0
forvalues i=1996(1)2005{
xtile expintens_quant_temp = exportintenslag if year == `i', n(4)
qui replace expintens_quant_annual = expintens_quant_temp if year == `i'
drop expintens_quant_temp
di "done with year: "  `i'
}
tab expintens_quant_annual, missing
*
corr expintens_quant_annual expintens_quant
drop  expintens_quant
rename expintens_quant_annual expintens_quant
*
reg llaborpty_d1  ltradeshock6 linv_d1 lmatraw_d1 i.naf2yr if expintens_quant==4, robust cluster(firmid)
global beta_q4 = _b[ltradeshock6]
*What the 4th quartile represents:
replace totexport = totexport/1000000000
table expintens_quant year, c(sum totexport) row col
table expintens_quant year, c(sum employment) row col
*
egen emp_quant = sum(employment), by(expintens_quant year)
egen emp_year = sum(employment), by(year)
gen emp_quant_shr = emp_quant/emp_year
table expintens_quant year, c(mean emp_quant_shr) 
*
save temp2_bottomline,replace
*******************************
*Calculating employment shares*
collapse (sum) employment , by(expintens_quant naf2 year)
rename emp emp
replace expintens_quant = 0 if expintens_quant==.
reshape wide emp , i(year naf2) j(expintens_quant)
foreach x in emp0 emp1 emp2 emp3 emp4{
replace `x' = 0 if `x' ==.
}
gen emp_sector = emp0+emp1+emp2+emp3+emp4
egen emp_year = sum(emp_sector), by(year)
save temp4_bottomline,replace
gen emp_sector_shr = emp_sector/emp_year
gen emp_quant4_sector_shr = emp4/emp_sector
collapse (mean) emp_sector_shr emp_quant4_sector_shr, by(naf2)
save temp5_bottomline,replace
use temp4_bottomline, clear
collapse (sum) emp4 (mean) emp_year, by(year)
gen emp_quant4_sector_shr = emp4/emp_year
collapse (mean)  emp_quant4_sector_shr
gen naf2=00
gen emp_sector_shr = 1
append using temp5_bottomline
replace emp_sector_shr = round(100*(emp_sector_shr),.01)
replace emp_quant4_sector_shr = round(100*(emp_quant4_sector_shr),.01)
order naf2
rename emp_sector_shr sec_shr
rename emp_quant4_sector_shr q4_shr
save temp5_bottomline,replace
erase temp4_bottomline.dta
******************************
*
use temp2_bottomline,clear
*
drop if expintens_quant!=4
*
replace ltradeshock6 = 0 if expintens_quant!=4
sort firmid year
*
by firmid: gen tradeshock_level =  100
*
by firmid: gen tradeshock_pred =  tradeshock_level*exp(ltradeshock6)
by firmid: gen llaborpty_pred = $beta_q4 * ltradeshock6+L1.llaborpty 
*
gen employment_lag = L1.employment
gen llaborpty_lag = L1.llaborpty 
*keep only observations where we have t and t-1
tab year
drop if L1.employment==. | L1.llaborpty==. | llaborpty_pred ==. | employment==.
tab year
*
*recalculate emp shares
drop emp_sum emp_shr
egen emp_sum = sum(employment) , by(year)
egen emp_sum_lag = sum(employment_lag) , by(year)
gen emp_shr = employment/emp_sum
gen emp_shr_lag = employment_lag/emp_sum_lag
*
gen laborpty_pred_emp = exp(llaborpty_pred)*emp_shr
gen laborpty_lag = exp(llaborpty_lag)*emp_shr_lag
*
gen tradeshock_pred_emp = tradeshock_pred*emp_shr
gen tradeshock_lag = tradeshock_level*emp_shr_lag
*
save temp2_bottomline,replace
*
*
corr ltradeshock6 D.employment
*
collapse (sum) laborpty_pred_emp laborpty_lag ///
employment employment_lag tradeshock_pred_emp tradeshock_lag, by(year) 
gen ratio_pty = laborpty_pred_emp/laborpty_lag
gen ratio_tradeshock = tradeshock_pred_emp/tradeshock_lag
*
*This is the aggregate effect, year by year, to be multiplied by the share of the 4th quartile
list
sum ratio_tradeshock
di "Average annual tradeshock for the 4th quartile of export intensity: " 100*(r(mean)-1) 
sum ratio_pty
di "Average annual effect on productivity for the 4th quartile of export intensity: " 100*(r(mean)-1) 
collapse (mean) ratio_pty ratio_tradeshock
replace ratio_pty = round(100*(ratio_pty-1),.01)
replace ratio_tradeshock = round(100*(ratio_tradeshock-1),.01)
gen name ="Total mfg"
gen naf2=00
save temp3_bottomline,replace

**************
*sector level*
**************
use temp2_bottomline, clear 
*recalculate emp shares
drop emp_sum* emp_shr*  laborpty_pred_emp laborpty_lag tradeshock_pred_emp tradeshock_lag
*
egen emp_sum = sum(employment) , by(naf2 year)
egen emp_sum_lag = sum(employment_lag) , by(naf2 year)
gen emp_shr = employment/emp_sum
gen emp_shr_lag = employment_lag/emp_sum_lag
*
gen laborpty_pred_emp = exp(llaborpty_pred)*emp_shr
gen laborpty_lag = exp(llaborpty_lag)*emp_shr_lag
*
gen tradeshock_pred_emp = tradeshock_pred*emp_shr
gen tradeshock_lag = tradeshock_level*emp_shr_lag
*
collapse (sum) laborpty_pred_emp laborpty_lag tradeshock_pred_emp tradeshock_lag, by(naf2 year)
*
gen ratio_pty = laborpty_pred_emp/laborpty_lag
gen ratio_tradeshock = tradeshock_pred_emp/tradeshock_lag
*
collapse (mean) ratio_pty ratio_tradeshock, by(naf2)
*
sum ratio_tradeshock
di "Average annual tradeshock for the 4th quartile of export intensity: " 100*(r(mean)-1) 
sum ratio_pty
di "Average annual effect on productivity for the 4th quartile of export intensity: " 100*(r(mean)-1) 
*
replace ratio_pty = round(100*(ratio_pty-1),.01)
replace ratio_tradeshock = round(100*(ratio_tradeshock-1),.01)
*
merge 1:1 naf2 using data/naf2names
drop  _merge 
gsort -ratio_pty
append using temp3_bottomline
order name naf2 ratio_pty 
merge 1:1 naf2 using temp5_bottomline
replace _merge =4 if naf2 == 0
gsort _merge -ratio_pty
drop _merge 
rename ratio_pty pty_rat
rename ratio_tradeshock tradeshock_rat
drop naf2
*
format  %9.2f pty_rat tradeshock q4 sec_
list if name != "Total mfg"
listtex  if name != "Total mfg" using $DIRLOG/Replication/tab-Bottomline.tex, replace rstyle(tabular) 
list if name == "Total mfg"
listtex  if name == "Total mfg" using $DIRLOG/Replication/tab-Bottomline-tot.tex, replace rstyle(tabular) 
